<?php
namespace app\controller;

use app\BaseController;
use app\service\LoginService;
use app\utils\CaptchaUtils;
use app\validate\LoginValidate;
use think\exception\ValidateException;
use \think\facade\Request;
use think\captcha\facade\Captcha;
use think\facade\Config;

class Login extends BaseController
{

    /**
     * 获取验证码接口
     * @return void
     *
     */
    public function captcha(){
        //todo 验证码应该和手机号绑定到一起
        if (Config::get('captcha.open_captcha')==2){

            returnFail(21012,'获取验证码失败');
        }
        $captcha = CaptchaUtils::create(3);
        returnSuccess(200,'获取成功',$captcha);

    }

    //合作商后台登录 tms
    public function login()
    {

        $param['mobile']     = Request::param('mobile/s', '');
        $param['password']     = Request::param('password/s', '');
        $param['captcha_id']   = Request::param('captcha_id/s', '');
        $param['captcha_value'] = Request::param('captcha_value/s', '');
        $param['login_ip']=Request::ip();
        $param['login_channel']=1;

        try {
            validate(LoginValidate::class)->check([
                'mobile'=> $param['mobile'],
                'password'=>$param['password'],
                'captcha_id'=>$param['captcha_id'],
                'captcha_value'=>$param['captcha_value']
            ]);
        }catch (ValidateException $e){

            returnFail(21009,$e->getError());
        }

        if (Config::get('captcha.open_captcha')==1){
            $captcha_check = CaptchaUtils::check($param['captcha_id'], strtolower($param['captcha_value']));
            if (empty($captcha_check)) {
                returnFail(21012,'验证码错误');
            }
        }
        $data =LoginService::login($param);
        returnSuccess(200,'登录成功',$data);



     }

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }



    //合作商后台登录 wms
    public function wms_login()
    {

        $param['mobile']     = Request::param('mobile/s', '');
        $param['password']     = Request::param('password/s', '');
        $param['captcha_id']   = Request::param('captcha_id/s', '');
        $param['captcha_value'] = Request::param('captcha_value/s', '');
        $param['login_ip']=Request::ip();
        $param['login_channel']=2;
        try {
            validate(LoginValidate::class)->check([
                'mobile'=> $param['mobile'],
                'password'=>$param['password'],
                'captcha_id'=>$param['captcha_id'],
                'captcha_value'=>$param['captcha_value']
            ]);
        }catch (ValidateException $e){

            returnFail(21009,$e->getError());
        }

//        if (Config::get('captcha.open_captcha')==1){
//            $captcha_check = CaptchaUtils::check($param['captcha_id'], $param['captcha_value']);
//            if (empty($captcha_check)) {
//                returnFail(21012,'验证码错误');
//            }
//        }

        $data =LoginService::login($param);
        returnSuccess(200,'登录成功',$data);



    }


}
